package com.drama.api.sys.role.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.drama.api.base.*;
import com.drama.api.sys.authority.domain.vo.SysAuthorityTreeSearchVO;
import com.drama.api.sys.authority.service.ISysAuthorityService;
import com.drama.api.sys.role.domain.vo.SysRoleAddVO;
import com.drama.api.sys.role.domain.vo.SysRoleSearchVO;
import com.drama.api.sys.role.domain.vo.SysRoleUpdateVO;
import com.drama.api.sys.role.domain.vo.SysRoleVO;
import com.drama.api.sys.role.service.ISysRoleService;
import com.drama.util.BindingResultUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@Api( tags = ApiIndex.SysRoleController)
@RequestMapping(value = "/api/sys/role")
@RestController
@SaCheckLogin
public class SysRoleController {

    @Autowired
    private ISysRoleService service;
    @Autowired
    private ISysAuthorityService authorityService;

    @ApiOperation(value = "查询平台角色列表")
    @GetMapping(value = "/list")
    public R<RestPage<List<SysRoleVO>>> list(
            @ApiParam(value = "查询参数") @ModelAttribute SysRoleSearchVO searchVO) {
        List<SysRoleVO> list = service.getList(searchVO);
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }

    @ApiOperation(value = "创建平台角色")
    @PostMapping(value = "/add")
    public R create(@ApiParam(value = "添加参数") @Valid @RequestBody SysRoleAddVO vo, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            service.add(vo);
        }
        return result;
    }

    @ApiOperation(value = "删除平台角色")
    @PostMapping(value = "/delete")
    public R delete( @ApiParam(value = "修改参数") @Valid @RequestBody IDEntity delete) {
        service.delete(delete.getId());
        return new R();
    }


    @ApiOperation(value = "修改平台角色")
    @PostMapping(value = "/update")
    public R update(@ApiParam(value = "修改参数") @Valid  @RequestBody SysRoleUpdateVO updateVO, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            service.update(updateVO);
        }
        return result;
    }





}
